<?php

namespace addons\config\user\controller;

use web\common\model\sys\SysParameterModel;

class Parameter extends \web\user\controller\AddonUserBase{
    
    public function index() {
        if (IS_POST) {
            $json = $_POST['json'];
            $data = json_decode($json, true);
            $m = new SysParameterModel;
            foreach ($data as $key => $val) {
                $id = $m->getID($key);
                if ($id > 0) {
                    $model['id'] = $id;
                    $model['parameter_val'] = $val;
                }
                $ret = $m->save($model);
                if($id==206){
                    $quM = new \addons\config\model\Quotation;
                    $datas = $quM->order('create_at desc')->find();
                    $datas['price_now'] = $val;
                    $quM->save($datas);
                }
            }
            (\think\Cache::connect(\think\Config::get('global_cache')))->set('sys_version',[]);
            //添加管理日志
            $this->addLog(self::LOG_EDIT, 'Parameter', $data, 0, '修改系统参数');
            return $this->successData();
        } else {
            $this->assign('id', '1');
            $m = new SysParameterModel;
            $list = $m->getParameterGroup();
            $this->assign('param_list', $list);
            $this->setLoadDataAction('loadData');
            return $this->fetch();
        }
    }

    public function loadData() {
        $m = new SysParameterModel;
        $data = $m->getDataList();
        return $data;
    }


    public function level_that() {
        if (IS_POST) {
            $data = $_POST;
            $m = new SysParameterModel;
            $model['id'] = 242;
            $model['parameter_val'] = $data['parameter_val'];
            $ret = $m->save($model);
            return $this->successData();
        } else {
            $this->assign('id', '1');
            $this->setLoadDataAction('loadDatas');
            return $this->fetch();
        }
    }

    public function loadDatas() {
        $m = new SysParameterModel;
        $data = $m->where(['type' => 10])->find();
        $data['parameter_val'] = htmlspecialchars_decode(($data['parameter_val']));
        return $data;
    }

    public function show_video(){
        $m = new SysParameterModel;
        if (IS_POST) {
            $data = $_POST;
            $str = str_replace('[', '', $data['obj_id']);
            $str = str_replace(']', '', $str);
            $ret = $m->where('field_name','video_list')->update(['parameter_val'=>$str]);
            if(!$ret) return $this->failData('视频更新失败');
            return $this->successData();
        } else {
            $this->assign('id', '264');
            $arr = $m->where('field_name','video_list')->value('parameter_val');
            $filter = ' ';
            if($arr){
                $filter = ' and id in ('.$arr.')';
            }
            $rows = (new \addons\video\model\Video)->getDataList(-1, -1, $filter,'id,video_title,video_img');
            $this->assign('video_list', json_encode($rows));
            return $this->fetch();
        }
    }


    /**
     * 设置视频内容
     */
    public function select_video(){
        // if (IS_POST) {
        //     $data = $_POST;
        //     $m = new SysParameterModel;
        //     $ret = $m->where('field_name','video_list')->update($data);
        //     return $this->successData();
        // } else {
            // $this->assign('id', '264');
            // $this->setLoadDataAction('loadVideo');
            return $this->fetch('select_video');
        // }
    }


    /**
     * 加载选择商品
     */
    public function loadVideo() {
        $keyword = $this->_get('keyword');
        $filter = ' ';
        if ($keyword != null) {
            $filter .= ' and video_title like \'%' . $keyword . '%\'';
        }
        $videoM = new \addons\video\model\Video();
        $rows = $videoM->getDataList(-1, -1, $filter,'id,video_title,video_img');
        $total = count($rows);
        return $this->toDataGrid($total, $rows);
    }
}
